﻿using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using Microsoft.SolverFoundation.Services;
using DigitalRune.Windows.TextEditor.Highlighting;

namespace SolverFoundationGUI {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();

            this.tbSource.Document.HighlightingStrategy
                = HighlightingManager.Manager.FindHighlighter("OML");            
        }

        private Solution solution = null;

        private string source = string.Empty;

        public void bw_DoWork(object sender, DoWorkEventArgs e) {
            StringReader reader = new StringReader(source);
            SolverContext context = SolverContext.GetContext();
            context.LoadModel(FileFormat.OML, reader);
            solution = context.Solve();
        }

        public void Run() {
            try {
                SolverContext.GetContext().ClearModel();
                StringReader reader = new StringReader(this.tbSource.Text);
                SolverContext context = SolverContext.GetContext();
                context.LoadModel(FileFormat.OML, reader);
                ModelReport mr = context.CheckModel();

                if (!mr.IsValid) {
                    MessageBox.Show(
                        "The model is not valid, please see output window for detail.");
                    this.tbOutput.Text = mr.ToString();

                    return;
                }

                Solution solution = context.Solve();

                Report report = solution.GetReport();
                this.tbOutput.Text = report.ToString();
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }

        public void bw_RunWorkerCompleted(
            object sender, RunWorkerCompletedEventArgs e) {
            if (solution != null) {
                Report report = solution.GetReport();
                this.tbOutput.Text = report.ToString();
            }
        }

        private void runToolStripMenuItem2_Click(object sender, EventArgs e) {
            //string source = this.tbSource.Text;

            //BackgroundWorker bw = new BackgroundWorker();

            //bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            //bw.RunWorkerCompleted 
            //    += new RunWorkerCompletedEventHandler(
            //        bw_RunWorkerCompleted);

            //bw.RunWorkerAsync();
            Run();
        }

        private string fileName = string.Empty;

        private void openToolStripMenuItem_Click(object sender, EventArgs e) {
            OpenFileDialog f = new OpenFileDialog();

            f.Filter = "OML file (*.oml)|*.oml";
            if (f.ShowDialog() == DialogResult.OK) {
                fileName = f.FileName;
                this.tbSource.Text = File.ReadAllText(f.FileName);
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e) {
            if (!string.IsNullOrEmpty(this.fileName)) {
                this.tbSource.SaveFile(this.fileName);
            } else {
                SaveFileDialog f = new SaveFileDialog();

                f.Filter = "OML file (*.oml)|*.oml";
                if (f.ShowDialog() == DialogResult.OK) {
                    this.fileName = f.FileName;
                    this.tbSource.SaveFile(f.FileName);
                }
            }
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) {
            SaveFileDialog f = new SaveFileDialog();

            f.Filter = "OML file (*.oml)|*.oml";
            if (f.ShowDialog() == DialogResult.OK) {
                this.fileName = f.FileName;
                this.tbSource.SaveFile(f.FileName);
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
            this.Close();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
            AboutBox1 x = new AboutBox1();

            x.Show();
        }

        private void validateToolStripMenuItem_Click(object sender, EventArgs e) {
            try {
                SolverContext.GetContext().ClearModel();
                StringReader reader = new StringReader(source);
                SolverContext context = SolverContext.GetContext();
                context.LoadModel(FileFormat.OML, reader);
                ModelReport report = context.CheckModel();

                if (report.IsValid) {
                    MessageBox.Show("The model is valid.");
                } else {
                    MessageBox.Show("The model is not valid, please see output window for detail.");
                    this.tbOutput.Text = report.ToString();
                }
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
